#define HZ coord.X=x
#define ZZ coord.Y=y
#define SCP SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord)
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<memory.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h> 
char ditu[300][300];
struct node{
int x,y;
}shen[1000];
char xiang[4]={'w','a','s','d'}; 
void donghua();
void guize();
int kaishi();
int youxi(int a,int b);
int xuan();
void jieshu();
int main()
{
	COORD coord;
	int x,y,xx,lp,kp,nan[4]={300,400,200,100};
	system("color 74");
	guize();
	donghua();
	while (1)
	{
		lp=kaishi();
		if (lp==2)
		{
			printf("\n\n\n谢谢来玩红红火火火贪吃蛇!!\n欢迎下次游玩啊客官-.-\n");
			return 0;
		}
		xx=xuan();
		kp=youxi(nan[xx],xx);
		x=51;y=14;
	    HZ;ZZ;SCP;
	    printf("            ");
		if (kp==-1)
		{
			x=51;y=12;
			HZ;ZZ;SCP;
			printf("游戏中退出-.-请重新选择...");
		}
		else
		{
			x=51;y=12;
			HZ;ZZ;SCP;
			printf("你此次游戏得分为：%d",kp*10*xx);
		}
	}
	Sleep(100000);
	return 0;
}
int xuan()
{
	COORD coord;
	int x,y,xx;
	for (x=0;x<50;x++)
	for (y=0;y<24;y++)
	{
		HZ;ZZ;SCP;
		printf(" ");
	}
	x=0;y=0;HZ;ZZ;SCP;
	printf("请选择游戏困难程度:\n");
	printf("一，小虫模式\n");
	printf("二，般蛇模式\n");
	printf("三，大龙模式\n");
	scanf("%d",&xx);
	return xx;
}
void donghua()//开始动画 
{
	COORD coord;
	int x=20,y=11,i,R=24;
	HZ;ZZ;SCP;
	printf("红红火火火贪吃蛇!"); 
	for (i=0;i<10;i++)
	{
		x=i;
		for (y=x;y<R-x;y++)
		{
			coord.X=x*2;
			coord.Y=y;
			SCP;
			printf("0 ");
		}
		x=(23-i);
		for (y=i;y<=x;y++)
		{
			coord.X=x*2;
			coord.Y=y;
			SCP;
			printf("0 "); 
		}
		x=i;
		for (y=x;y<R-x;y++)
		{
			coord.Y=x;
			coord.X=y*2;
			SCP;
			printf("0 ");
		}
		x=(23-i);
		for (y=i;y<=x;y++)
		{
			coord.Y=x;
			coord.X=y*2;
			SCP;
			printf("0 ");
		}
		Sleep(100);
	}
	return ;
}
void guize()
{
	COORD coord;
	int x=50,y;
	for (y=0;y<26;y++)
	{
		HZ;ZZ;SCP;
		printf("|");
	}
	x=52;y=0;
	HZ;ZZ;
	SCP;
	printf("游戏规则如下:");
	x=52;y=2;
	HZ;ZZ;SCP;
	printf("一，用wasd四个键来控制移动方向");
	y=3;HZ;ZZ;SCP;
	printf("向.");
	x=52;y=4;
	HZ;ZZ;SCP;
	printf("二，当贪吃蛇撞上边界或自己的时候");
	y=5;HZ;ZZ;SCP;
	printf("死亡.");
	x=52;y=6;
	HZ;ZZ;SCP;
	printf("三，游戏进行时可以按下空格去解一个小手");
	x=52;y=8;
	HZ;ZZ;SCP;
	printf("四，上厕所回来后可以按1键继");
	y=9;HZ;ZZ;SCP;
	printf("续，按'0'键会回到刚开始游戏");
	y=10;HZ;ZZ;SCP;
	printf("时的选择界面重新开始.");
}
int kaishi()
{
	COORD coord;
	int x,y,lp;
	for (x=0;x<50;x++)
	for (y=0;y<24;y++)
	{
		HZ;ZZ;SCP;
		printf(" ");
	}
	x=0;y=0;HZ;ZZ;SCP;
	printf("一，游戏开始\n");
	printf("二，游戏结束\n");
	printf("请选择:");
	scanf("%d",&lp);
	return lp;
}
int youxi(int a,int b)
{
	
	int x,y;
	char ma[60][30];
	COORD coord;
	int	i,R=50,D=24,ll=0,fen=0,p,wu=0,wux,wuy,fafe=0;
	char zhiling,kp;
	for (x=0;x<60;x++)
	for (y=0;y<30;y++)
	ma[x][y]=' ';
	for (x=0;x<50;x++)
	for (y=0;y<25;y++)
	{
		HZ;ZZ;SCP;
		printf(" ");
	}
	y=24;
	for (x=0;x<50;x++)
	{
		HZ;ZZ;SCP;
		printf("="); 
	}
	shen[ll].x=rand()%8+20;
	shen[ll++].y=rand()%6+10;
	x=shen[ll-1].x;y=shen[ll-1].y;
	HZ;ZZ;SCP;
	ditu[x][y]='+';
	printf("+");
	p=rand()%4;
	if (xiang[p]=='w')
	{
		for (i=0;i<3;i++)
		{
			shen[ll].x=shen[ll-1].x;
			shen[ll].y=shen[ll-1].y-1;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
		}
	}
	else if (xiang[p]=='a')
	{
		for (i=0;i<3;i++)
		{
			shen[ll].x=shen[ll-1].x-1;
			shen[ll].y=shen[ll-1].y;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
		}
	}
	else if (xiang[p]=='s')
	{
		for (i=0;i<3;i++)
		{
			shen[ll].x=shen[ll-1].x;
			shen[ll].y=shen[ll-1].y+1;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
		}
	}
	else if (xiang[p]=='d')
	{
		for (i=0;i<3;i++)
		{
			shen[ll].x=shen[ll-1].x+1;
			shen[ll].y=shen[ll-1].y;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
		}
	}
	while (1)
	{
		wux=rand()%R;
		wuy=rand()%D;
		if (ditu[wux][wuy]!='+')
		{
			x=wux;y=wuy;
			HZ;ZZ;SCP;ditu[x][y]='#';
			printf("#");
			break;
		}
	}
	while (1)
	{
		x=51;y=12;
		HZ;ZZ;SCP;
		printf("按任意方向键开始游戏...");
		if(kbhit())
		{
			kp=getch();
			if (kp=='w'||kp=='a'||kp=='s'||kp=='d')
			{
				x=51;y=12;
			    HZ;ZZ;SCP;
			    printf("                            ");
			    break;
			}
		}
		Sleep(100);
    }
	while (1)
	{
		x=51;y=14;
	    HZ;ZZ;SCP;
	    printf("得分：%d",fen*10*b);
		if(kbhit())
	    zhiling=getch();
		if (zhiling!=xiang[p])
		{
			if (p<2&&zhiling!=xiang[p+2]||p>1&&zhiling!=xiang[p-2])
			{
				if (zhiling=='w')
				p=0;
				else if (zhiling=='a')
				p=1;
				else if (zhiling=='s')
				p=2;
				else if (zhiling=='d')
				p=3;
				else if (zhiling==' ')//暂停 
				{
					zhiling='1';//改变一下-.-不然一直暂停... 
					while (1)
					{
						if(kbhit())
						{
							kp=getch();
		                    if (kp=='1')
		                    break;
		                    else if (kp=='0')
		                    {
		                    	return -1;
							}
						}
	                    
						Sleep(100);
					}
				}
			}
			
		}
		if (xiang[p]=='w')
		{
			shen[ll].x=shen[ll-1].x;
			shen[ll].y=shen[ll-1].y-1;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			if (x==-1||x==R||y==-1||y==D||ditu[x][y]=='+')
			{
				fafe=1;
				break;
			}
			if (ditu[x][y]=='#')
			{
				fen++;
				wu=1;
			}
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
			if (!wu)
			{
				x=shen[0].x;y=shen[0].y;
				HZ;ZZ;SCP;ditu[x][y]=' ';
		        printf(" ");
				for (i=0;i<ll-1;i++)
				shen[i]=shen[i+1];
				ll--;
			}
		}
		else if (xiang[p]=='a')
		{
			shen[ll].x=shen[ll-1].x-1;
			shen[ll].y=shen[ll-1].y;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			if (x==-1||x==R||y==-1||y==D||ditu[x][y]=='+')
			{
				fafe=1;
				break;
			}
			if (ditu[x][y]=='#')
			{
				fen++;
				wu=1;
			}
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
			if (!wu)
			{
				x=shen[0].x;y=shen[0].y;
				HZ;ZZ;SCP;ditu[x][y]=' ';
		        printf(" ");
				for (i=0;i<ll-1;i++)
				shen[i]=shen[i+1];
				ll--;
			}
		}
		else if (xiang[p]=='s')
		{
			shen[ll].x=shen[ll-1].x;
			shen[ll].y=shen[ll-1].y+1;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			if (x==-1||x==R||y==-1||y==D||ditu[x][y]=='+')
			{
				fafe=1;
				break;
			}
			if (ditu[x][y]=='#')
			{
				fen++;
				wu=1;
			}
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
			if (!wu)
			{
				x=shen[0].x;y=shen[0].y;
				HZ;ZZ;SCP;ditu[x][y]=' ';
		        printf(" ");
				for (i=0;i<ll-1;i++)
				shen[i]=shen[i+1];
				ll--;
			}
		}
		else if (xiang[p]=='d')
		{
			shen[ll].x=shen[ll-1].x+1;
			shen[ll].y=shen[ll-1].y;
			ll++;x=shen[ll-1].x;y=shen[ll-1].y;
			if (x==-1||x==R||y==-1||y==D||ditu[x][y]=='+')
			{
				fafe=1;
				break;
			}
			if (ditu[x][y]=='#')
			{
				fen++;
				wu=1;
			}
			HZ;ZZ;SCP;ditu[x][y]='+';
			printf("+");
			if (!wu)
			{
				x=shen[0].x;y=shen[0].y;
				HZ;ZZ;SCP;ditu[x][y]=' ';
		        printf(" ");
				for (i=0;i<ll-1;i++)
				shen[i]=shen[i+1];
				ll--;
			}
		}
		if (wu)
		{
			while (1)
			{
				wux=rand()%R;
				wuy=rand()%D;
				if (ditu[wux][wuy]!='+')
				{
					x=wux;y=wuy;
					HZ;ZZ;SCP;ditu[x][y]='#';
					printf("#");
					break;
				}
			}
			wu=0;
		}
		if (fafe)
		break;
    	Sleep(a);
	}
	jieshu();
	return fen;
}
void jieshu()
{
	COORD coord;
	int xx=20,yy=11,x,y,i,j;
	for (i=0;i<6;i++)
	{
		x=xx+i;
		for (j=0;j<3;j++)
		{
			y=yy+j;
			HZ;ZZ;SCP;
			printf(" ");
		}
	}
	x=20;y=11;HZ;ZZ;SCP;
	printf("……游戏失败");
	Sleep(2000);
}